//
//  ST_GiftTabCell.swift
//  btvideo
//
//  Created by mac on 2020/6/28.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_GiftTabCell: UICollectionViewCell {
    
    static let landscapeEdge = 10.cgFloat
    
    static let height = ST_GiftItemView.height * 2 + 5
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        
        
        for row in 0 ..< ST_GiftItemView.rowCount{
            for column in 0 ..< ST_GiftItemView.columnCount{
                let itemView = ST_GiftItemView()
                itemView.isHidden = true
                contentView.addSubview(itemView)
                
                itemView.snp.makeConstraints { (make) in
                    if column == 0{
                        make.left.equalToSuperview().offset(ST_GiftTabCell.landscapeEdge)
                    }else{
                        let offset = ST_GiftItemView.width+ST_GiftItemView.gap
                        make.left.equalToSuperview().offset(offset*column.cgFloat+ST_GiftTabCell.landscapeEdge)
                    }
                    let offsetY = ST_GiftItemView.height + ST_GiftItemView.gap
                    make.top.equalToSuperview().offset(offsetY*row.cgFloat)
                    make.width.equalTo(ST_GiftItemView.width)
                    make.height.equalTo(ST_GiftItemView.height)
                }
                
                itemView.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
                
            }
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    var model = GiftPageModel(){
        didSet{
            
             
            for (idx,item) in contentView.subviews.enumerated(){
                
                if let v = item as? ST_GiftItemView,let item_model = model.pageArray[safe:idx]{
                    v.child_model = item_model
                    item.isHidden = false
                }else{
                    item.isHidden = true
                }
                
            }
            
        }
    }
    
    
    var selected_closure:((ST_BTGiftModel)->())?
    
    @objc fileprivate func click_handle(sender:ST_GiftItemView){
        
        if sender.isSelected{
            return
        }
        
        sender.child_model?.isSelected = true
        
        guard let m = sender.child_model else{
            return
        }
        selected_closure?(m)
        
    }
    
}




//MARK: - 会员
class ST_GiftMemberTabCell: UICollectionViewCell{
    
    static let landscapeEdge = 10.cgFloat
    
    static let height = ST_GiftMemberItemView.height * 2 + 5
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        
        
        for row in 0 ..< ST_GiftMemberItemView.rowCount{
            for column in 0 ..< ST_GiftMemberItemView.columnCount{
                let itemView = ST_GiftMemberItemView()
                itemView.isHidden = true
                contentView.addSubview(itemView)
                
                itemView.snp.makeConstraints { (make) in
                    if column == 0{
                        make.left.equalToSuperview().offset(ST_GiftMemberTabCell.landscapeEdge)
                    }else{
                        let offset = ST_GiftMemberItemView.width+ST_GiftMemberItemView.horizontalGap
                        make.left.equalToSuperview().offset(offset*column.cgFloat+ST_GiftMemberTabCell.landscapeEdge)
                    }
                    let offsetY = ST_GiftMemberItemView.height + ST_GiftMemberItemView.verticalGap
                    make.top.equalToSuperview().offset(offsetY*row.cgFloat)
                    make.width.equalTo(ST_GiftMemberItemView.width)
                    make.height.equalTo(ST_GiftMemberItemView.height)
                }
                itemView.addTarget(self, action: #selector(click_handle(sender:)), for: UIControl.Event.touchUpInside)
            }
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    var model = GiftPageModel(){
        didSet{
            
             
            for (idx,item) in contentView.subviews.enumerated(){
                
                if let v = item as? ST_GiftMemberItemView,let item_model = model.pageArray[safe:idx]{
                    v.child_model = item_model
                    item.isHidden = false
                }else{
                    item.isHidden = true
                }
                
            }
            
        }
    }
    
    var selected_closure:((ST_BTGiftModel)->())?
    
    @objc fileprivate func click_handle(sender:ST_GiftMemberItemView){
        
        if sender.isSelected{
            return
        }
        
        sender.child_model?.isSelected = true
        
        guard let m = sender.child_model else{
            return
        }
        selected_closure?(m)
        
    }    
}

